package com.pjh.whonumber;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class DatabaseHelper extends SQLiteOpenHelper {
	 
    private final Context myContext;
    private static DatabaseHelper mInstance;
    private static SQLiteDatabase myWritableDb;
    private static String DB_NAME = "myContact"; 
 
    /**
     * Constructor takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     *
     * @param context
     *            the application context
     */
    public DatabaseHelper(Context context) {
 
        super(context, DB_NAME, null, 2);
        this.myContext = context;
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
    	// TODO Auto-generated method stub
    	  db.execSQL(DataBases.CreateDB._CREATE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    	// TODO Auto-generated method stub
    	
    }
 

    public void dropTable(){
    	SQLiteDatabase db = this.getWritableDatabase();

    	db.execSQL("drop table myContact");
    }

    public void addContact(String name,String tel){
    	 SQLiteDatabase db = this.getWritableDatabase();
    	 
//         ContentValues values = new ContentValues();
//         values.put("name", name); // Contact Name
//         values.put("tel", tel); // Contact Phone
//  
         // Inserting Row
         
         db.execSQL("insert into myContact(name,tel) values('"+name+"','"+tel+"')");
         //db.insert("myContact", null, values);
//         db.close(); // Closing database connection
    }
    /**
     * Get default instance of the class to keep it a singleton
     *
     * @param context
     *            the application context
     */
    public static DatabaseHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new DatabaseHelper(context);
        }
        return mInstance;
    }
 
    /**
     * Returns a writable database instance in order not to open and close many
     * SQLiteDatabase objects simultaneously
     *
     * @return a writable instance to SQLiteDatabase
     */
    public SQLiteDatabase getMyWritableDatabase() {
        if ((myWritableDb == null) || (!myWritableDb.isOpen())) {
            myWritableDb = this.getWritableDatabase();
        }
 
        return myWritableDb;
    }
 
    @Override
    public void close() {
        super.close();
        if (myWritableDb != null) {
            myWritableDb.close();
            myWritableDb = null;
        }
    }
    
	public final class DataBases {
	     
	    public  final class CreateDB implements BaseColumns{
	        public static final String NAME = "name";
	        public static final String tel = "tel";
	        public static final String _TABLENAME = "myContact";
	        public static final String _CREATE = 
	            "create table  "+_TABLENAME+" ("
	                    +_ID+" integer primary key autoincrement, "    
	                    +NAME+" text not null , "
	                    +tel+" text not null ); ";
	    }
	}
}